<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>修改密码</title>
<link rel="stylesheet" href="layui/css/layui.css">
<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
<style>
    .content{
        padding-top:10px;
    }
    .button{
        margin-top: 20px;
    }
    .content .layui-form-label{
        margin-top: 10px;
    }
    .content .layui-input-block{
        width: 75%;
        margin-top: 10px;
    }
    .error{
        color:red;
        display: none;
    }
    .two{
        display: none;
    }
</style>
</head>
<body>
<div class="layui-layout layui-layout-admin">
    <!--form表单-->
    <div class="layui-form one">
        <!--标题-->
        <div class="layui-form-item content" id="content">
            <label class="layui-form-label">原密码：</label>
            <div  class="layui-input-block">
                <input type="password" id="oldpassword" name="password"  required lay-verify="required" placeholder="请输入原密码" autocomplete="off" class="layui-input">
            </div>
        </div>

        <!--操作-->
        <div class="layui-form-item" id="operating">
            <div class="layui-input-block">
                <button class="layui-btn button" lay-submit lay-filter="formDemo1">提交</button>
                <button  class="layui-btn layui-btn-primary button exit" id="exit1" value="取消">取消</button>
            </div>
        </div>
    </div>

    <div class="layui-form two">
        <!--标题-->
        <div class="layui-form-item content">
            <label class="layui-form-label">新密码：</label>
            <div  class="layui-input-block">
                <input type="password" id="password" name="password"  required lay-verify="required" placeholder="请输入新密码" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">确认密码：</label>
            <div  class="layui-input-block">
                <input type="password" id="repassword" name="repassword"  required lay-verify="required" placeholder="请再次输入密码" autocomplete="off" class="layui-input">
                <p class="error">两次密码输入不一致</p>
            </div>
        </div>

        <!--操作-->
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn button" lay-submit lay-filter="formDemo2">提交</button>
                <button  class="layui-btn layui-btn-primary button exit" id="exit2" value="取消">取消</button>
            </div>
        </div>
    </div>
</div>
<script src="layui/layui.js"></script>
<script src="js/public.js"></script>
<script>
    var header = {
        userID: window.localStorage.getItem("userID")
    }
    var flag = false;
    $("input[name=repassword]").on('input',function () {
        var re = $("input[name=repassword]").val();
        var password = $("input[name=password]").val();
        if (re===password){
            flag = true;
            $(".error").css("display","none");
        }else{
            flag = false;
            $(".error").css("display","block");
        }
    })

    layui.use(['form','element','layer'],function () {
        var form = layui.form;
        var element = layui.element;
        var layer = layui.layer;
        //点击取消，关闭页面
        $(".exit").click(function () {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        });

        form.on('submit(formDemo1)',function (data) {
            console.log("data1:"+data);
            var parm=data.field;
            parm.userID = localStorage.getItem("userID");

            $.ajax({
                url:'http://localhost:8086/sharedBikes_war_exploded/auth/verify',
                type:"POST",
                headers:header,
                contentType: 'application/json',
                data:JSON.stringify(parm),
                success:function (res) {
                    console.log("updateRes:"+res)
                    if (res.status==0){
                       if(res.data==1){
                          $(".one").remove();
                          $(".two").show();
                       }else{
                           layer.msg("身份验证失败！请重新输入！")
                       }
                    } else if (res.status==-2) {
                        layer.confirm('身份过期，重新登录',{btn:['知道了'],
                            cancel: function(index, layero){
                                //取消操作，点击右上角的X
                                parent.location.href = "login.html";
                            }},function(index){
                            parent.location.href = "login.html";
                        });
                    }else{
                       layer.msg("出现了未知的错误！")
                    }
                }
            })

        })

        form.on('submit(formDemo2)',function (data) {
            console.log(data);
            var parm = data.field;
            parm.userID = localStorage.getItem("userID");
            if (flag){
                $.ajax({
                    url:'http://localhost:8086/sharedBikes_war_exploded/auth/updatePassword',
                    type:"post",
                    headers:header,
                    contentType: 'application/json',
                    data:JSON.stringify(parm),
                    success:function (res) {
                        if (res.status==0){
                            if(res.data>0){
                                layer.confirm('修改成功',{btn:['知道了']}, function(index){
                                    var index = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(index);
                                });
                            }else{
                                layer.msg("未进行修改");
                            }
                        } else if (res.status==-2) {
                            layer.confirm('身份过期，重新登录',{btn:['知道了'],
                                cancel: function(index, layero){
                                    //取消操作，点击右上角的X
                                    parent.location.href = "login.html";
                                }},function(index){
                                parent.location.href = "login.html";
                            });
                        }else{
                            layer.msg("出现了未知的错误！");
                        }
                    }
                })
            }

        });
    });
</script>
</body>
</html>
